<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="dialogVisible=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delListFn(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="pageConfig.pagesize"
                :total="listTotal"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="CompanyInfo.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="CompanyInfo.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="CompanyInfo.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="CompanyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="formData.id?'编辑用户':'新增用户'"
        :visible.sync="dialogVisible"
        @close="cancelFn"
      >
        <el-form ref="addRole" :model="formData" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formData.description" autocomplete="off" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button size="small" type="primary" @click="addListFn">确认</el-button>
          <el-button size="small" @click="cancelFn">取消</el-button>
        </template>
      </el-dialog>

      <!-- 权限弹窗 -->
      <el-dialog title="分配权限" :visible="isShowPerm" @close="isShowPerm=false">
        <el-checkbox-group v-model="permIds">
          <el-tree :data="permList" :props="{label:'name'}" default-expand-all :expand-on-click-node="false">
            <template v-slot="scope">
              <el-checkbox :label="scope.data.id">
                {{ scope.data.name }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>
        <template slot="footer">
          <el-row type="flex" justify="center">
            <el-button type="primary" size="small" @click="confirmPerm">确认</el-button>
            <el-button size="small" @click="isShowPerm=false">取消</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, addRoleList, delRoleList, editRoleList, getRoleDetail, addPerm } from '@/api/setting'
import { tree } from '@/utils'
import { getPermList } from '@/api/permission'
export default {
  data() {
    return {
      CompanyInfo: {},
      pageConfig: {
        page: 1,
        pagesize: 5
      },
      isShowPerm: false,
      roleList: [], // 角色列表
      listTotal: 1, // 角色总数量
      dialogVisible: false, // 对话框是否显示
      permList: [],
      permIds: [],
      roleID: '',
      // 角色新增数据
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    this.CompanyInfo = await getCompanyInfo(this.$store.getters.companyId)
    console.log('CompanyInfo', this.CompanyInfo)
    this.getList()
    console.log(this.roleList)
  },
  methods: {
    // 分页功能
    async handleCurrentChange(val) {
      console.log(val)
      this.pageConfig.page = val
      // 发送请求获取用户列表
      this.getList()
    },
    // 获取用户列表
    async getList() {
      // 发送请求
      const { rows, total } = await getRoleList(this.pageConfig)
      this.roleList = rows
      this.listTotal = total
      console.log('用户列表', this.roleList)
    },
    // 新增用户
    async addListFn() {
      // 校验表单
      try {
        await this.$refs.addRole.validate()
        // 发送请求
        // 如果有id 编辑功能，如果没有id删除功能
        if (this.formData.id) {
          await editRoleList(this.formData)
        } else {
          await addRoleList(this.formData)
        }
        // 关闭弹窗
        this.dialogVisible = false
        // 提示用户
        this.$message.success('操作成功')
        // 刷新页面
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 取消新增
    cancelFn() {
      this.dialogVisible = false
      // 清空表单内容和校验规则，只能清除默认的数据，不能清除额外的数据
      this.formData = {
        name: '',
        description: ''
      }
      this.$refs.addRole.resetFields()
    },
    // 编辑功能
    async editRole(id) {
      this.dialogVisible = true
      // 发送请求获取数据
      this.formData = await getRoleDetail(id)
      console.log('新formData', this.formData)
    },

    // 删除功能
    async delListFn(id) {
      try {
        // 二次确认
        await this.$confirm('确定删除?')
        // 发送请求
        await delRoleList(id)
        // 如果数据剩下一条，删完之后该页已经没有数据了，将页数-1获取数据
        if (this.roleList.length === 1 && this.pageConfig.page > 1) {
          this.pageConfig.page--
        }
        // 刷新页面
        this.getList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 分配权限
    async assignPerm(id) {
      // 树形列表渲染
      this.permList = await tree(await getPermList(), '0')
      const res = await getRoleDetail(id)
      this.permIds = res.permIds
      this.isShowPerm = true
      this.roleID = id
    },
    // 确认分配权限
    async confirmPerm() {
      await addPerm({
        id: this.roleID,
        permIds: this.permIds
      })
      this.$message.success('操作成功')
      this.isShowPerm = false
    }
  }
}
</script>

<style>

</style>
